package com.sudo.common.component;

import com.sudo.common.constant.UserConstant;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

@Component
public final class RedisKeys {

    @Resource
    private ExtraProperties extraProperties;

    //用户 token标识符
    public static String REDIS_USER_TOKEN;
    //角色对应的用户
    public static String REDIS_ROLE_USER;
    public static String REDIS_ROLE_PERMISSION;
    public static String REDIS_MENU_BUTTON;

    public static String REDIS_USER_BEHAVIOR;
    public static String REDIS_SYS_INFO;

    // 图片验证码
    public static String REDIS_PICTURE_VERIFY_CODE;
    public static String REDIS_EMAIL_VERIFY_CODE;

    // 幂等性
    public static String REDIS_IDEMPOTENCE;

    // 接口幂等性
    public static String REDIS_API_IDEMPOTENCE;

    // 用户Api 限流
    private static String USER_API_LIMIT;

    // 用户webSite
    private static String USER_WEB_LIMIT;

    @PostConstruct
    public void init() throws Exception {
        REDIS_USER_TOKEN = extraProperties.getAppName()+":redis_user_token";
        REDIS_ROLE_USER = extraProperties.getAppName()+":redis_role_user";
        REDIS_ROLE_PERMISSION = extraProperties.getAppName() + ":redis_role_permission";
        REDIS_MENU_BUTTON = extraProperties.getAppName() + ":redis_menu_button";
        REDIS_USER_BEHAVIOR = extraProperties.getAppName() + ":redis_user_behavior";
        REDIS_SYS_INFO = extraProperties.getAppName() +":redis_sys_info";
        REDIS_PICTURE_VERIFY_CODE = extraProperties.getAppName() + ":redis_picture_verify_code";
        REDIS_EMAIL_VERIFY_CODE = extraProperties.getAppName() + ":redis_email_verify_code";
        REDIS_IDEMPOTENCE = extraProperties.getAppName() + ":redis_idempotence";
        REDIS_API_IDEMPOTENCE = extraProperties.getAppName() + ":redis_api_idempotence";
        USER_API_LIMIT = extraProperties.getAppName() + ":user_api_limit";
        USER_WEB_LIMIT = extraProperties.getAppName() + ":user_web_limit";
    }


    // Redis key
    public String redisMenuButton(String menuCode){
        return RedisKeys.REDIS_MENU_BUTTON + ":" + menuCode;
    }
    public String redisUserToken(String username){
        return RedisKeys.REDIS_USER_TOKEN + ":" + username;
    }

    public String redisRoleUser(String roleCode){
        return RedisKeys.REDIS_ROLE_USER + ":" + roleCode;
    }

    public String redisRolePermission(String roleCode, String menuCode){
        return RedisKeys.REDIS_ROLE_PERMISSION + ":" + roleCode + ":" + menuCode;
    }
    public String redisPictureVerifyCode(String uniqueClient){
        return RedisKeys.REDIS_PICTURE_VERIFY_CODE + ":" + uniqueClient;
    }
    public String redisEmailVerifyCode(String uniqueClient){
        return RedisKeys.REDIS_EMAIL_VERIFY_CODE + ":" + uniqueClient;
    }

    public String redisIdempotence(String uniqueCode){
        return RedisKeys.REDIS_IDEMPOTENCE + ":" + uniqueCode;
    }

    public String getMemLogRedisKey(String serverNode){
        return RedisKeys.REDIS_SYS_INFO+":"+ UserConstant.MEM+":"+serverNode;
    }

    public String getMemLogRedisRealTimeKey(String serverNode){
        return RedisKeys.REDIS_SYS_INFO +":"+ UserConstant.MEM+":realtime:"+serverNode;
    }

    public String getCpuLogRedisKey(String serverNode){
        return RedisKeys.REDIS_SYS_INFO +":"+ UserConstant.CPU+":"+serverNode;
    }

    public String getCpuLogRedisRealTimeKey(String serverNode){
        return RedisKeys.REDIS_SYS_INFO +":"+ UserConstant.CPU+":realtime:"+serverNode;
    }

    public String getMemCpuRedisRealTimeKey(String serverNode){
        return RedisKeys.REDIS_SYS_INFO+":realtime:"+serverNode;
    }

    public String getRedisApiIdempotentKey(String username,String className, String methodName) {
        return RedisKeys.REDIS_API_IDEMPOTENCE + ":" + username + ":" + className + "#" + methodName;
    }

    public String userApiLimit(String usernameOrIp ,String clazzName,String methodName){
        return USER_API_LIMIT + ":" + usernameOrIp + ":" + clazzName + "#" + methodName;
    }

    public String userWebLimit(String usernameOrIp){
        return USER_WEB_LIMIT + ":" + usernameOrIp;
    }
}
